package com.hermit.qmst.tools;

import android.app.PendingIntent;
import android.content.Context;
import android.os.Bundle;
import android.os.RemoteException;
import android.text.TextUtils;

import com.hermit.qmst.MainActivity;
import com.hermit.qmst.csipsimple.api.ISipService;
import com.hermit.qmst.csipsimple.api.SipProfile;
import com.hermit.qmst.csipsimple.utils.CallHandlerPlugin;

/**
 * Created by hermithermit on 15/4/21.
 */
public class SipCallHelp {

    private static final String TAG = "SipCallHelp";
    private static ISipService mISipService;
    private static Preferences preferences;
    public static void placeCallWithOption(Bundle b,Context pContext ,String pNumber) {

        mISipService = MainActivity.getInstance().mISipService;
        preferences= new Preferences(pContext);
        if (mISipService == null) {
            Log.i(TAG, "service is null",true);
            return;
        }
        String toCall = "";
        Long accountToUse = SipProfile.INVALID_ID;
        // Find account to use
        accountToUse = (long) Common.mSipAccountId;

        // Find number to dial
        toCall =	Utils.analysePhoneNumber(pNumber);

        if (TextUtils.isEmpty(toCall)) {
            return;
        }

        // Well we have now the fields, clear theses fields
        // -- MAKE THE CALL --//
        if (accountToUse >= 0) {
            // It is a SIP account, try to call service for that
            try {
                mISipService.makeCallWithOptions(toCall, accountToUse.intValue(), b);
                Log.i(TAG, "makeCallWithOptions",true);
            } catch (RemoteException e) {
                Log.i(TAG, "Service can't be called to make the call",true);
            }
        } else if (accountToUse != SipProfile.INVALID_ID) {
            // It's an external account, find correct external account

            CallHandlerPlugin ch = new CallHandlerPlugin(pContext);
            ch.loadFrom(accountToUse, toCall, new CallHandlerPlugin.OnLoadListener() {
                @Override
                public void onLoad(CallHandlerPlugin ch) {
                    Log.i(TAG, "placePluginCall",true);
                    placePluginCall(ch ,mISipService);
                }
            });
        }


//        if (preferences.getPreferenceBooleanValue(Preferences.CALL_WAIT_RING)) {
//            Intent musicIntent = new Intent(pContext, MediaPlayService.class);
//            pContext.startService(musicIntent);
//        }
    }

    private static void placePluginCall(CallHandlerPlugin ch ,ISipService service) {

        try {
            String nextExclude = ch.getNextExcludeTelNumber();
            if (service != null && nextExclude != null) {
                try {
                    service.ignoreNextOutgoingCallFor(nextExclude);
                } catch (RemoteException e) {
                    Log.i(TAG, "Impossible to ignore next outgoing call", true);
                    e.printStackTrace();
                }
            }
            ch.getIntent().send();
        } catch (PendingIntent.CanceledException e) {
            Log.i(TAG, "Pending intent cancelled" ,true );
            e.printStackTrace();
        }
    }
}
